package com.telenav.snowsweeper;

import java.util.Random;

import com.telenav.snowsweeper.controller.MapEdgeTouchEventHandler;
import com.telenav.snowsweeper.data.EdgeMap;
import com.telenav.snowsweeper.data.EdgeMap.InitException;
import com.telenav.snowsweeper.data.PointsManager;
import com.telenav.snowsweeper.net.ICallBack;
import com.telenav.snowsweeper.net.IRequest;
import com.telenav.snowsweeper.net.ThreadPool;
import com.telenav.snowsweeper.renderer.MapEdgeRenderer;
import com.telenav.snowsweeper.renderer.Util;
import com.telenav.snowsweeper.renderer.MapEdgeBuilder;
import com.telenav.snowsweeper.renderer.SurfaceRenderer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Process;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends Activity implements ICallBack
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        UrlsOfServer.getInsttance().loadServerConfig(this);
        Button quitBt = (Button) this.findViewById(R.id.quit);
        quitBt.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                ThreadPool.getInstance().stop();
                MainActivity.this.finish();
                Process.killProcess(Process.myPid());
            }
        });
        Button newGameBt = (Button) this.findViewById(R.id.start_game);
        newGameBt.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
//                ThreadPool.getInstance().addJob(new MapDataRequest(MainActivity.this, 1));
//                ThreadPool.getInstance().addJob(new PassPointRequest(MainActivity.this, 1, 0));
//                ThreadPool.getInstance().addJob(new HintRequest(MainActivity.this, 1, 1, 2, null));
//                ThreadPool.getInstance().addJob(new EvaluationRequest(MainActivity.this, 1));
            	init();
            	
            	enterGame();
            }
        });
    }
    
    private void init() {
    	Util.init(this);
    	PointsManager.init(Util.ROW + 1, Util.COLUMN + 1);
    }
    
    private void enterGame() {
    	LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.activity_game, null);
        setContentView(vg);
        
        SurfaceView sv = (SurfaceView) vg.findViewById(R.id.mapcanvas);
        
        EdgeMap edgeMap = fakeEdgeMap();
        MapEdgeRenderer edgeRender = new MapEdgeRenderer(new MapEdgeBuilder(edgeMap));
        edgeRender.getRenderParams().move2(200, 200);
        edgeRender.getRenderParams().setScaleXY(128, 128);
        SurfaceRenderer render = new SurfaceRenderer(sv);
        render.addObjectRenderer(edgeRender);
        new MapEdgeTouchEventHandler(this, sv, edgeRender.getRenderParams());
    }
    
    private EdgeMap fakeEdgeMap() {
    	int[][] vPosTransTimes = new int[Util.ROW][Util.COLUMN + 1];
    	int[][] vNegTransTimes = new int[Util.ROW][Util.COLUMN + 1];
    	int[][] hPosTransTimes = new int[Util.ROW + 1][Util.COLUMN];
    	int[][] hNegTransTimes = new int[Util.ROW + 1][Util.COLUMN];
    	
    	fakeData(vPosTransTimes);
    	fakeData(vNegTransTimes);
    	fakeData(hPosTransTimes);
    	fakeData(hNegTransTimes);
    	
    	try {
			return new EdgeMap(vPosTransTimes, vNegTransTimes, hPosTransTimes, hNegTransTimes);
		}
		catch (InitException e) {
			e.printStackTrace();
		}
		return null;
    }
    
    private void fakeData(int[][] data) {
    	Random r = new Random();
    	for (int i = 0; i < data.length; ++i) {
    		for (int j = 0; j < data[i].length; ++j) {
    			data[i][j] = r.nextInt(3);
    		}
    	}
    }
    
    @Override
    protected void onPause()
    {
        super.onPause();
        ThreadPool.getInstance().stop();
    }

    @Override
    protected void onResume()
    {
        ThreadPool.getInstance().start();
        super.onResume();
    }

    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item)
    {
        Intent ins = new Intent(MainActivity.this, SettingActivity.class);
        startActivity(ins);
        return true;
    }

    @Override
    public boolean parseData(IRequest quest, String reponse)
    {
        Log.e("MainActivity", quest.getClass().getName() + ":" + reponse);
        return true;
    }

    public void failedRequest(IRequest quest)
    {

    }
}
